/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eng.scc.core.editors;

import com.eng.scc.core.data.Device;
import com.eng.scc.core.data.TruckModel;
import com.eng.scc.core.data.Vehicle;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.parts.DecimalTextField;
import com.eng.scc.core.parts.DeviceTable;
import com.eng.scc.core.view.ApplicationContext;
import java.awt.Component;
import java.awt.Dimension;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.eng.scc.core.parts.FormattedTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Rafael
 */
public class VehicleEditor extends AbstractPanelEditor<Vehicle>
{
    
    public static void main( String[] args )
    {
        try
        {
            VehicleEditor.edit(null, new Vehicle());
        }
        catch ( Exception ex )
        {
            Logger.getLogger( VehicleEditor.class.getName() ).log( Level.SEVERE, null, ex );
        }
    }
    
    public static boolean edit( Component parent, Vehicle v ) throws Exception
    {
        VehicleEditor ce = new VehicleEditor();

        ce.setSource( v );
        
        ce.populateFields();
        
        boolean acpt = DefaultEditor.createEditor( parent, 
                                           "Editor de Veículo", 
                                           "Formulário de preenchimento de dados do Veículo", 
                                           new Dimension( 700, 500 ), ce );
        
        return acpt;
    }
    
    @Override
    public void setSource( Vehicle source )
    {
        if ( source == null )
        {
            throw new IllegalArgumentException( "Vehicle cannot be null!" );
        }

        this.source = source;
    }
    
    private void populateFields()
    {
        try
        {
            if ( source.getCompanyId() > 0 )
            {
                companySelector.setSource( FuelControllerManagerService.getInstance().getCompany( source.getCompanyId() ) );
            }
            
            if ( source.getDriverId() > 0 )
            {
                driverSelector.setSource( FuelControllerManagerService.getInstance().getDriver(source.getDriverId() ) );
            }
            
            tfBrand.setText( source.getBrand());
            tfModel.setText( source.getModel());
            tfRenavam.setText( source.getRenavam() );
            tfPlate.setText( source.getPlate() );
            tfChassi.setText( source.getChassi() );
            dtMaxFuelCapability.setValue( source.getMaxFuel() );
            dtAverage.setValue( source.getManufacturingAvg() );
            colorSelector.setColor( source.getColor() );
            spAxels.setValue( source.getAxles() );
            dtManufacturingYear.setText( source.getManufacturingYear() + "/" + source.getModelYear() );
            taInfo.setText( source.getInfo() );
            
            dtCapacity.setValue( source.getCapacity());
            cbTruck.setSelectedIndex(source.getTruckModel().getId());

            if ( source.getId() == 0 )
            {
                panels.removeTabAt( 1 );
            }
            
            else
            {
                deviceTable.setDevices( FuelControllerManagerService.getInstance().getDevices( source.getId() ) );
            }
        }
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }

    @Override
    public void validateEditor( com.eng.scc.core.util.Error error )
    {        
        if ( companySelector.getSource() == null )
        {
            error.add( "Informe a Empresa vínculada a este Veículo" );
        }
        
        if ( tfBrand.getText().trim().isEmpty() )
        {
            error.add( "Informe a Marca" );
        }
        
        if ( tfModel.getText().trim().isEmpty() )
        {
            error.add( "Informe o Modelo" );
        }
        
        if ( tfPlate.getText().trim().isEmpty() )
        {
            error.add( "Informe a Placa" );
        }
        
        if ( dtManufacturingYear.getText().trim().isEmpty() )
        {
            error.add( "Informe Ano e Modelo" );
        }
        else 
        {
            String year = dtManufacturingYear.getText().substring( 0, dtManufacturingYear.getText().lastIndexOf( "/" ) ).trim();
            String model = dtManufacturingYear.getText().substring( dtManufacturingYear.getText().lastIndexOf( "/" ) + 1, dtManufacturingYear.getText().length() ).trim();

            if ( year.isEmpty() || model.isEmpty() )
            {
                error.add( "Informe Ano e Modelo" );
            }
            else if ( Integer.valueOf( year ) > Integer.valueOf( model ) )
            {
                error.add( "Ano do veículo deve ser menor ou igual ao seu Modelo" );
            }
            else if ( Integer.valueOf( year ) < 1950 )
            {
                error.add( "Ano deve ser Maior que 1950" );
            }
            else if ( Integer.valueOf( model ) < 1950 )
            {
                error.add( "Modelo deve ser Maior que 1950" );
            }
        }
        
        if ( colorSelector.getColor().equals( String.valueOf( getBackground().getRGB() ) ) )
        {
            error.add( "Selecione a Cor do Veículo" );
        }
        
        if ( ((Integer)spAxels.getValue()) == 0 )
        {
            error.add( "Informe a quantidade de Eixos" );
        }
        
        if ( dtMaxFuelCapability.getValue() == 0 )
        {
            error.add( "Informe a Capacidade Máxima de Combustível" );
        }
        
        if ( dtAverage.getValue() == 0 )
        {
            error.add( "Informe a Média especificada pelo Fabricante ou obtida pela Empresa" );
        }
        
        if ( dtCapacity.getValue() == 0 )
        {
            error.add( "Informe a Capacidade Máxima da Carroceria" );
        }
    }
    
    @Override
    public void getSource()
    {
        source.setCompanyId( companySelector.getSource().getId() );
        
        if ( driverSelector.getSource() != null )
        {
            source.setDriverId( driverSelector.getSource().getId() );
        }
        
        else
        {
            source.setDriverId( 0 );
        }
        
        source.setBrand( tfBrand.getText() );
        source.setModel( tfModel.getText() );
        source.setRenavam( tfRenavam.getText() );
        source.setPlate( tfPlate.getText() );
        source.setChassi( tfChassi.getText() );
        source.setMaxFuel( dtMaxFuelCapability.getValue() );
        source.setManufacturingAvg( dtAverage.getValue() );
        source.setColor( colorSelector.getColor() );
        source.setAxles( (Integer)spAxels.getValue() );
        source.setManufacturingYear( Integer.valueOf( dtManufacturingYear.getText().substring( 0, dtManufacturingYear.getText().lastIndexOf( "/" ) ) ) );
        source.setModelYear( Integer.valueOf( dtManufacturingYear.getText().substring( dtManufacturingYear.getText().lastIndexOf( "/" ) + 1, dtManufacturingYear.getText().length() ) )  );
        source.setInfo( taInfo.getText() );
        source.setTruckModel( (TruckModel)cbTruck.getSelectedItem() );
        source.setCapacity(dtCapacity.getValue());
    }
    
    /**
     * Creates new form VehicleEditor
     */
    public VehicleEditor()
    {
        initComponents();
        driverSelector.setEditable( false );
        companySelector.setEditable( false );
        
        initMasks();
    }

    private void deleteDevice()
    {
        try
        {
            if ( deviceTable.getSelectedDevice() != null  )
            {
                if ( JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog( this, "Deseja realmente remover esse dispositivo?" ) )
                {
                    FuelControllerManagerService.getInstance().deleteDevice( deviceTable.getSelectedDevice() );
                    
                    deviceTable.setDevices( FuelControllerManagerService.getInstance().getDevices( source.getId() ) );
                }
            }

            else
            {
                JOptionPane.showConfirmDialog( this, "Selecione um dispositivo!" );
            }
        }
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private void editDevice()
    {
        try
        {
            Device d = deviceTable.getSelectedDevice();

            if ( d != null )
            {
                if ( DeviceEditor.edit( this, d ) )
                {
                    FuelControllerManagerService.getInstance().updateDevice( d );

                    deviceTable.setDevices( FuelControllerManagerService.getInstance().getDevices( source.getId() ) );
                }
            }
            
            else
            {
                JOptionPane.showConfirmDialog( this, "Selecione um dispositivo!" );
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private void addDevice()
    {
        try
        {
            Device d = new Device();
            
            if ( DeviceEditor.edit( this, d ) )
            {
                d.setVehicleId( source.getId() );
                
                FuelControllerManagerService.getInstance().addDevice( d );
                
                deviceTable.setDevices( FuelControllerManagerService.getInstance().getDevices( source.getId() ) );
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        panels = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        lbBrand = new javax.swing.JLabel();
        tfBrand = new javax.swing.JTextField();
        lbModel = new javax.swing.JLabel();
        tfModel = new javax.swing.JTextField();
        lbPlate = new javax.swing.JLabel();
        tfPlate = new FormattedTextField();
        lbYearModel = new javax.swing.JLabel();
        dtManufacturingYear = new FormattedTextField();
        lbColor = new javax.swing.JLabel();
        lbChassi = new javax.swing.JLabel();
        tfChassi = new javax.swing.JTextField();
        lbRenavam = new javax.swing.JLabel();
        tfRenavam = new FormattedTextField();
        lbMaxCapFuel = new javax.swing.JLabel();
        dtMaxFuelCapability = new DecimalTextField();
        jLabel13 = new javax.swing.JLabel();
        lbAxels = new javax.swing.JLabel();
        lbAvg = new javax.swing.JLabel();
        dtAverage = new DecimalTextField();
        jLabel16 = new javax.swing.JLabel();
        spAxels = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        colorSelector = new com.eng.scc.core.parts.ColorSelector();
        panelHardwares = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        deviceTable = new DeviceTable();
        tbEdit = new javax.swing.JButton();
        tbDelete = new javax.swing.JButton();
        tbAdd = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taInfo = new javax.swing.JTextArea();
        lbCompany = new javax.swing.JLabel();
        companySelector = new com.eng.scc.core.parts.CompanySelector();
        lbDriver = new javax.swing.JLabel();
        driverSelector = new com.eng.scc.core.parts.DriverSelector();

        setLayout(new java.awt.GridBagLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        lbBrand.setText("Marca:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbBrand, gridBagConstraints);

        tfBrand.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(tfBrand, gridBagConstraints);

        lbModel.setText("Modelo:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbModel, gridBagConstraints);

        tfModel.setPreferredSize(new java.awt.Dimension(130, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(tfModel, gridBagConstraints);

        lbPlate.setText("Placa:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbPlate, gridBagConstraints);

        tfPlate.setMinimumSize(new java.awt.Dimension(60, 20));
        tfPlate.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(tfPlate, gridBagConstraints);

        lbYearModel.setText("Ano / Modelo:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbYearModel, gridBagConstraints);

        dtManufacturingYear.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(dtManufacturingYear, gridBagConstraints);

        lbColor.setText("Cor:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbColor, gridBagConstraints);

        lbChassi.setText("Nº Chassi:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbChassi, gridBagConstraints);

        tfChassi.setMinimumSize(new java.awt.Dimension(200, 20));
        tfChassi.setPreferredSize(new java.awt.Dimension(200, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(tfChassi, gridBagConstraints);

        lbRenavam.setText("Renavam:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbRenavam, gridBagConstraints);

        tfRenavam.setMinimumSize(new java.awt.Dimension(130, 20));
        tfRenavam.setPreferredSize(new java.awt.Dimension(130, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(tfRenavam, gridBagConstraints);

        lbMaxCapFuel.setText("Cap. Máx. Combustível:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbMaxCapFuel, gridBagConstraints);

        dtMaxFuelCapability.setMinimumSize(new java.awt.Dimension(50, 20));
        dtMaxFuelCapability.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(dtMaxFuelCapability, gridBagConstraints);

        jLabel13.setText("Litros(L)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jLabel13, gridBagConstraints);

        lbAxels.setText("Eixos:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbAxels, gridBagConstraints);
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(new JLabel("Tipo de Carroceria:"), gridBagConstraints);
        
        cbTruck.setMinimumSize(new java.awt.Dimension(150, 20));
        cbTruck.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(cbTruck, gridBagConstraints);
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(new JLabel("Cap. Máxima Carroceria (t):"), gridBagConstraints);
        
        dtCapacity.setMinimumSize(new java.awt.Dimension(100, 20));
        dtCapacity.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(dtCapacity, gridBagConstraints);

        lbAvg.setText("Média Fabricante:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(lbAvg, gridBagConstraints);

        dtAverage.setMinimumSize(new java.awt.Dimension(50, 20));
        dtAverage.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(dtAverage, gridBagConstraints);

        jLabel16.setText("km/L");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jLabel16, gridBagConstraints);

        spAxels.setModel( new SpinnerNumberModel(1, 0, 20, 1));
        spAxels.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(spAxels, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jLabel5, gridBagConstraints);

        javax.swing.GroupLayout colorSelector1Layout = new javax.swing.GroupLayout(colorSelector);
        colorSelector.setLayout(colorSelector1Layout);
        colorSelector1Layout.setHorizontalGroup(
            colorSelector1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        colorSelector1Layout.setVerticalGroup(
            colorSelector1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(colorSelector, gridBagConstraints);

        panels.addTab("Geral", jPanel1);

        panelHardwares.setLayout(new java.awt.GridBagLayout());
        
        jScrollPane2.setViewportView(deviceTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelHardwares.add(jScrollPane2, gridBagConstraints);

        tbEdit.setText("Editar");
        tbEdit.setMaximumSize(new java.awt.Dimension(90, 23));
        tbEdit.setMinimumSize(new java.awt.Dimension(90, 23));
        tbEdit.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelHardwares.add(tbEdit, gridBagConstraints);

        tbDelete.setText("Excluir");
        tbDelete.setMaximumSize(new java.awt.Dimension(90, 23));
        tbDelete.setMinimumSize(new java.awt.Dimension(90, 23));
        tbDelete.setName(""); // NOI18N
        tbDelete.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelHardwares.add(tbDelete, gridBagConstraints);

        tbAdd.setText("Adicionar");
        tbAdd.setMaximumSize(new java.awt.Dimension(90, 23));
        tbAdd.setMinimumSize(new java.awt.Dimension(90, 23));
        tbAdd.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        panelHardwares.add(tbAdd, gridBagConstraints);

        panels.addTab("Dispositivos", panelHardwares);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        taInfo.setColumns(20);
        taInfo.setRows(5);
        jScrollPane1.setViewportView(taInfo);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jScrollPane1, gridBagConstraints);

        panels.addTab("Info", jPanel2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(panels, gridBagConstraints);

        lbCompany.setText("Empresa:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbCompany, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(companySelector, gridBagConstraints);

        lbDriver.setText("Motorista:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lbDriver, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(driverSelector, gridBagConstraints);
        
        tbAdd.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                addDevice();
            }
        });
        tbEdit.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                editDevice();
            }
        });
        tbDelete.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                deleteDevice();
            }
        });
    }
	
    private com.eng.scc.core.parts.ColorSelector colorSelector;
    private com.eng.scc.core.parts.CompanySelector companySelector;
    private com.eng.scc.core.parts.DriverSelector driverSelector;
    private javax.swing.JButton tbEdit;
    private javax.swing.JButton tbDelete;
    private javax.swing.JButton tbAdd;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner spAxels;
    private javax.swing.JTabbedPane panels;
    private DeviceTable deviceTable;
    private DecimalTextField dtMaxFuelCapability;
    private DecimalTextField dtAverage;
    private FormattedTextField dtManufacturingYear;
    private javax.swing.JLabel lbAvg;
    private javax.swing.JLabel lbAxels;
    private javax.swing.JLabel lbBrand;
    private javax.swing.JLabel lbChassi;
    private javax.swing.JLabel lbColor;
    private javax.swing.JLabel lbCompany;
    private javax.swing.JLabel lbDriver;
    private javax.swing.JLabel lbMaxCapFuel;
    private javax.swing.JLabel lbModel;
    private javax.swing.JLabel lbPlate;
    private javax.swing.JLabel lbRenavam;
    private javax.swing.JLabel lbYearModel;
    private javax.swing.JPanel panelHardwares;
    private javax.swing.JTextArea taInfo;
    private javax.swing.JTextField tfBrand;
    private javax.swing.JTextField tfChassi;
    private javax.swing.JTextField tfModel;
    private FormattedTextField tfPlate;
    private FormattedTextField tfRenavam;
    private JComboBox<TruckModel> cbTruck = new JComboBox<TruckModel>( TruckModel.values() );
    private DecimalTextField dtCapacity = new DecimalTextField();
    
    private void initMasks()
    {
        try
        {
            dtManufacturingYear.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "####/####" ) ) );
            tfRenavam.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "###########" ) ) );
            tfPlate.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "UUU####" ) ) );
        }
        
        catch ( ParseException e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
}
